// 门店数据相关API
// import request from '@/utils/request.js'

// 简单的请求函数，替代request工具
const request = (config) => {
	return new Promise((resolve) => {
		// 模拟API请求
		setTimeout(() => {
			resolve({ code: 200, data: null, message: 'success' });
		}, 500);
	});
}

// 获取门店数据列表
export function getStoreDataList(params) {
	return request({
		url: '/store/data/list',
		method: 'GET',
		data: params
	})
}

// 获取门店数据详情
export function getStoreDataDetail(id) {
	return request({
		url: `/store/data/detail/${id}`,
		method: 'GET'
	})
}

// 导出门店数据
export function exportStoreData(params) {
	return request({
		url: '/store/data/export',
		method: 'POST',
		data: params
	})
}

// 获取门店数据统计
export function getStoreDataStats(params) {
	return request({
		url: '/store/data/stats',
		method: 'GET',
		data: params
	})
}

// 模拟数据 - 获取门店数据列表
export function getMockStoreDataList(params = {}) {
	return new Promise((resolve) => {
		setTimeout(() => {
			const { page = 1, pageSize = 20, keyword = '', filter = 'all' } = params
			
			// 生成模拟数据
			const mockData = []
			const productNames = [
				'某某商品', '优质商品', '热销商品', '新品商品', '特价商品',
				'精选商品', '推荐商品', '畅销商品', '限量商品', '经典商品'
			]
			
			for (let i = 1; i <= 50; i++) {
				const purchaseCount = Math.floor(Math.random() * 2000) + 500
				const returnCount = Math.floor(Math.random() * 300)
				const netSales = purchaseCount - returnCount
				
				const item = {
					id: i,
					sequence: i,
					productName: productNames[Math.floor(Math.random() * productNames.length)],
					productCode: `SP${String(i).padStart(4, '0')}`,
					purchaseCount: purchaseCount,
					returnCount: returnCount,
					netSales: netSales,
					updateTime: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toLocaleString('zh-CN')
				}
				
				// 应用搜索筛选
				if (keyword) {
					const searchKeyword = keyword.toLowerCase()
					if (!item.productName.toLowerCase().includes(searchKeyword) && 
						!item.productCode.toLowerCase().includes(searchKeyword)) {
						continue
					}
				}
				
				// 应用分类筛选
				if (filter === 'hasReturn' && item.returnCount === 0) continue
				if (filter === 'noReturn' && item.returnCount > 0) continue
				
				mockData.push(item)
			}
			
			// 分页处理
			const startIndex = (page - 1) * pageSize
			const endIndex = startIndex + pageSize
			const list = mockData.slice(startIndex, endIndex)
			
			resolve({
				code: 200,
				message: '获取成功',
				data: {
					list: list,
					total: mockData.length,
					page: page,
					pageSize: pageSize,
					hasMore: endIndex < mockData.length
				}
			})
		}, 500)
	})
}

// 模拟数据 - 获取门店数据详情
export function getMockStoreDataDetail(id) {
	return new Promise((resolve) => {
		setTimeout(() => {
			const purchaseCount = Math.floor(Math.random() * 2000) + 500
			const returnCount = Math.floor(Math.random() * 300)
			
			resolve({
				code: 200,
				message: '获取成功',
				data: {
					id: id,
					sequence: id,
					productName: '某某商品',
					productCode: `SP${String(id).padStart(4, '0')}`,
					purchaseCount: purchaseCount,
					returnCount: returnCount,
					netSales: purchaseCount - returnCount,
					category: '日用品',
					supplier: '供应商A',
					unit: '件',
					costPrice: 25.50,
					salePrice: 35.00,
					profit: 9.50,
					profitRate: 27.14,
					stockQuantity: 150,
					minStock: 50,
					maxStock: 500,
					createTime: '2024-01-15 10:30:00',
					updateTime: new Date().toLocaleString('zh-CN'),
					remark: '这是一个热销商品，需要及时补货'
				}
			})
		}, 300)
	})
}

// 模拟数据 - 获取门店数据统计
export function getMockStoreDataStats(params = {}) {
	return new Promise((resolve) => {
		setTimeout(() => {
			resolve({
				code: 200,
				message: '获取成功',
				data: {
					totalProducts: 50,
					totalPurchase: 45000,
					totalReturn: 3500,
					totalNetSales: 41500,
					avgPurchasePerProduct: 900,
					avgReturnPerProduct: 70,
					returnRate: 7.78,
					topProducts: [
						{ name: '热销商品', purchaseCount: 1800, returnCount: 50 },
						{ name: '优质商品', purchaseCount: 1650, returnCount: 80 },
						{ name: '新品商品', purchaseCount: 1500, returnCount: 120 }
					],
					monthlyTrend: [
						{ month: '1月', purchase: 8000, return: 600 },
						{ month: '2月', purchase: 8500, return: 650 },
						{ month: '3月', purchase: 9000, return: 700 }
					]
				}
			})
		}, 400)
	})
}
